﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.Utilities;
using NetDon.OA.Interface.CRM;

namespace NetDon.OA.Portal.CRM
{
    public partial class CustomerInfoEdit : PageBase
    {
        #region Properties
        protected Guid CustomerId
        {
            get
            {
                return Parameters.GetGuid("CustomerId");
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CancelButton = btnReturn;


                if (CustomerId != Guid.Empty)
                {
                    var service = ServiceContainer.GetService<ICustomerInfoService>();
                    var entity = service.GetCustomerInfo(CustomerId);
                    panelCustomInfo.DataSource = entity;
                    panelCustomInfo.DataBind();
                    if (entity.IsChina.HasValue)
                    {
                        if (entity.IsChina.Value)
                        {
                            panelCustomInfo.Items["Province"].Visible = true;
                            panelCustomInfo.Items["City"].Visible = true;
                            panelCustomInfo.Items["Internation"].Visible = false;
                        }
                        else
                        {
                            panelCustomInfo.Items["Province"].Visible = false;
                            panelCustomInfo.Items["City"].Visible = false;
                            panelCustomInfo.Items["Internation"].Visible = true;
                        }
                        radioNation.SelectedValue = entity.IsChina.Value ? NationType.China.ToString() : NationType.Foreign.ToString();
                    }
                    if (entity.Business.HasValue)
                    {
                        ddlBusiness.SelectedIndex = ddlBusiness.Items.IndexOf(ddlBusiness.Items.FindByValue(entity.Business.ToString()));
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (radioNation.SelectedValue == NationType.China.ToString())
            {
                if (string.IsNullOrEmpty(ddlProvince.SelectedValue))
                {
                    Alert("请选择省份");
                    return;
                }
                if (string.IsNullOrEmpty(ddlCity.SelectedValue))
                {
                    Alert("请选择城市");
                    return;
                }
            }
            var customerId = CustomerId;
            if (CustomerId == Guid.Empty)
            {
                customerId = Guid.NewGuid();
            }

            var service = ServiceContainer.GetService<ICustomerInfoService>();

            bool existCode = false;
            existCode = service.CheckCustomerCodeExist(CustomerId, txtCustomerCode.Text.Trim(), CurrentApplicationContext.CompanyId);

            if (existCode)
            {
                Alert("客户编号已存在，请重新输入！");
                return;
            }

            bool existName = false;
            existName = service.CheckCustomerNameExist(CustomerId, txtCustomerName.Text.Trim(), CurrentApplicationContext.CompanyId);

            if (existName)
            {
                Alert("客户名称已存在，请重新输入！");
                return;
            }

            var data = new CustomerInfoData
                           {
                               CustomerId = customerId,
                               CustomerCode = txtCustomerCode.Text.Trim(),
                               CustomerName = txtCustomerName.Text.Trim(),
                               Business = ddlBusiness.SelectedValue.ToGuid(),
                               CustomerSource = txtCustomerSource.Text.Trim(),
                               ContactName = txtContactName.Text.Trim(),
                               ContactPosition = txtContactPosition.Text.Trim(),
                               Mobile = txtMobile.Text.Trim(),
                               Phone = txtPhone.Text.Trim(),
                               Fax = txtFax.Text.Trim(),
                               Email = txtEmail.Text.Trim(),
                               Address = txtAddress.Text.Trim(),
                               Zipcode = txtZipcode.Text.Trim(),
                               Website = txtWebsite.Text.Trim(),
                               Introduction = txtIntroduction.Text.Trim(),
                               Internation = txtInternation.Text.Trim(),
                               Remark = txtRemark.Text.Trim(),
                               AreaId =CurrentApplicationContext.CompanyId


                           };
            if (radioNation.SelectedValue == NationType.China.ToString())
            {
                data.Province = ddlProvince.SelectedValue;
                data.City = ddlCity.SelectedValue;
                data.IsChina = true;
            }
            else
            {
                data.Internation = txtInternation.Text.Trim();
                data.IsChina = false;
            }
            bool result = CustomerId == Guid.Empty ? service.AddCustomerInfo(data) : service.ModifyCustomerInfo(data);
            if (result)
            {
                Success("保存成功!");
                CloseWindow(true);
            }
            else
            {
                ShowDataError(data);
            }
        }


        protected void radioNation_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (radioNation.SelectedValue == NationType.China.ToString())
            {
                panelCustomInfo.Items["Province"].Visible = true;
                panelCustomInfo.Items["City"].Visible = true;
                panelCustomInfo.Items["Internation"].Visible = false;

            }
            else if (radioNation.SelectedValue == NationType.Foreign.ToString())
            {
                panelCustomInfo.Items["Province"].Visible = false;
                panelCustomInfo.Items["City"].Visible = false;
                panelCustomInfo.Items["Internation"].Visible = true;
            }
        }
    }
    public enum NationType
    {
        China = 1,
        Foreign = 2
    }
}